﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wexflow.Core.Service.Common;

namespace Wexflow.Core
{
    enum WeekDays
    {
        SUN = 7,
        MON = 1,
        TUE = 2,
        WED = 3,
        THU = 4,
        FRI = 5,
        SAT = 6
    }
    public class WeekHelper
    {

        public static string GetWeekDay(string dayStr)
        {
            string temp = "";
            string[] days = dayStr.Split(',');
            for (int i = 0; i < days.Length; i++)
            {
                switch (days[i])
                {
                    case "1":
                        temp += WeekDays.MON.ToString();
                        break;
                    case "2":
                        temp += WeekDays.TUE.ToString();
                        break;
                    case "3":
                        temp += WeekDays.WED.ToString();
                        break;
                    case "4":
                        temp += WeekDays.THU.ToString();
                        break;
                    case "5":
                        temp += WeekDays.FRI.ToString();
                        break;
                    case "6":
                        temp += WeekDays.SAT.ToString();
                        break;
                    case "7":
                        temp += WeekDays.SUN.ToString();
                        break;
                    default:
                        return "";
                }
            }
            return temp;
        }
        public static void CheckInterval(string w, IntervalType intervalType)
        {

            int temp = 0;
            bool c = false;
            string[] day = w.Split(',');
            //检查分割后的字符是否转换成数字
            for (int i = 0; i < day.Length; i++)
            {
                c = int.TryParse(day[i], out temp);
                if (!c)
                {
                    //c = false;
                    //break;
                    throw new Exception("DateConversion error, wrong date format！");
                }
            }
            if (intervalType == IntervalType.D1)
            {
                foreach (var item in day)
                {
                    int t=int.Parse(item);
                    if (t < 0 || t > 31) {
                        c = false;
                        break;
                    }
                }
            }
            if (intervalType == IntervalType.W)
            {
                foreach (var item in day)
                {
                    int t = int.Parse(item);
                    if (t < 0 || t > 7)
                    {
                        c = false;
                        break;
                    }
                }
            }
            if (intervalType == IntervalType.M) {
                //对月、日加入限制
                if (day == null || day.Length != 2)
                {
                    throw new Exception("DateConversion error, wrong date format！");
                }
                if (int.Parse(day[0]) < 0 || int.Parse(day[0]) >12) {
                    c = false;
                }
                if (int.Parse(day[1]) < 0 || int.Parse(day[1]) > 31)
                {
                    c = false;
                }
            }

            if (!c)
            {
                throw new Exception("DateConversion error, wrong date format！");
            }

        }
    }
}
